"use strict";
const common_vendor = require("../../../common/vendor.js");
const api_police_index = require("../../../api/police/index.js");
const api_police_clock = require("../../../api/police/clock.js");
const api_user = require("../../../api/user.js");
const api_community_index = require("../../../api/community/index.js");
const config_app = require("../../../config/app.js");
const utils_tools = require("../../../utils/tools.js");
const common_qqmapWxJssdk = require("../../../common/qqmap-wx-jssdk.js");
const stores_userInfo = require("../../../stores/userInfo.js");
if (!Array) {
  const _easycom_userOfTopLeft2 = common_vendor.resolveComponent("userOfTopLeft");
  const _easycom_up_swiper2 = common_vendor.resolveComponent("up-swiper");
  const _easycom_up_modal2 = common_vendor.resolveComponent("up-modal");
  const _easycom_u_modal2 = common_vendor.resolveComponent("u-modal");
  const _easycom_up_toast2 = common_vendor.resolveComponent("up-toast");
  (_easycom_userOfTopLeft2 + _easycom_up_swiper2 + _easycom_up_modal2 + _easycom_u_modal2 + _easycom_up_toast2)();
}
const _easycom_userOfTopLeft = () => "../../../components/userOfTopLeft/userOfTopLeft.js";
const _easycom_up_swiper = () => "../../../node-modules/uview-plus/components/u-swiper/u-swiper.js";
const _easycom_up_modal = () => "../../../node-modules/uview-plus/components/u-modal/u-modal.js";
const _easycom_u_modal = () => "../../../node-modules/uview-plus/components/u-modal/u-modal.js";
const _easycom_up_toast = () => "../../../node-modules/uview-plus/components/u-toast/u-toast.js";
if (!Math) {
  (_easycom_userOfTopLeft + _easycom_up_swiper + menuWithBg + _easycom_up_modal + _easycom_u_modal + _easycom_up_toast)();
}
const menuWithBg = () => "../../../components/menuWithBg/menuWithBg.js";
const _sfc_defineComponent = common_vendor.defineComponent({
  __name: "index",
  setup(__props) {
    const userInfoStore = stores_userInfo.useUserInfoStore();
    const current = common_vendor.ref(0);
    const data = common_vendor.reactive({
      userInfo: {},
      selectStatus: null,
      //0 在岗 1在岗-外出 2 在岗巡逻 3 不在岗(下班)
      showStatusList: false,
      showModal1: false,
      showModal2: false,
      showSubMsgPopup: false
    });
    const {
      userInfo,
      selectStatus,
      showStatusList,
      showModal1,
      showModal2,
      showSubMsgPopup
    } = common_vendor.toRefs(data);
    const uToastRef = common_vendor.ref(null);
    const swipers = common_vendor.reactive([
      "https://mscy.cqlandun.com:8088/static/police-banner1.png",
      "https://mscy.cqlandun.com:8088/static/police-banner2.png",
      "https://mscy.cqlandun.com:8088/static/police-banner3.png"
    ]);
    const menuList = common_vendor.ref([
      {
        title: "社区工作扫码打卡",
        bgColor: "darkGreen",
        badge: 0
      },
      {
        title: "每月工作提示",
        bgColor: "purple",
        badge: 0
      },
      {
        title: "办事咨询提示",
        bgColor: "yellow",
        badge: 0
      },
      {
        title: "群众评价",
        bgColor: "tangerine",
        badge: 0
      },
      {
        title: "群众投诉",
        bgColor: "azure",
        badge: 0
      },
      {
        title: "线上清廉",
        bgColor: "azure",
        badge: 0
      }
    ]);
    const onMenu = (item) => {
      if (item.title === "社区工作扫码打卡") {
        policeClock();
      }
      if (item.title === "每月工作提示") {
        common_vendor.index.navigateTo({
          url: "/subPages/police/workReminder/workReminder"
        });
      }
      if (item.title === "办事咨询提示") {
        common_vendor.index.navigateTo({
          url: "/subPages/police/serviceConsultation/index"
        });
      }
      if (item.title === "群众评价") {
        common_vendor.index.navigateTo({
          url: "/subPages/police/mobEvaluation/mobEvaluationTip"
        });
      }
      if (item.title === "群众投诉") {
        common_vendor.index.navigateTo({
          url: "/subPages/police/mobComplaint/mobComplaintTip"
        });
      }
      if (item.title === "线上清廉") {
        common_vendor.index.navigateTo({
          url: "/subPages/police/honesty/index"
        });
      }
    };
    const fileReport = () => {
      showStatusList.value = false;
      common_vendor.index.navigateTo({
        url: "/subPages/police/report/fileReport"
      });
    };
    const onNoReadStat = () => {
      common_vendor.index.showLoading({
        title: "加载中",
        mask: true
      });
      api_police_index.getNoReadCount({}).then((res) => {
        common_vendor.index.hideLoading();
        const { data: data2 } = res;
        menuList.value[1].badge = +data2.taskCount;
        menuList.value[2].badge = +data2.consultCount;
      }).catch((err) => {
        common_vendor.index.hideLoading();
        uToastRef.value.show({
          type: "error",
          message: err
        });
      });
    };
    const getUserInfo = () => {
      api_user.policeUserInfo({ policeUserId: userInfoStore.userInfo.userId }).then((res) => {
        const { data: data2 } = res;
        userInfo.value = data2;
        selectStatus.value = data2.dutyStatus;
      }).catch((err) => {
        uToastRef.value.show({
          type: "error",
          message: err
        });
      });
    };
    const editStatus = (status) => {
      if (status === userInfo.value.dutyStatus) {
        if (status === 3) {
          common_vendor.index.showToast({
            title: "已是下班状态",
            icon: "none",
            mask: true
          });
          showModal2.value = false;
        } else {
          common_vendor.index.showToast({
            title: "请选择状态",
            icon: "none",
            mask: true
          });
        }
      } else {
        api_police_index.setPoliceStatus({ dutyStatus: status }).then((_res) => {
          status === 3 ? showModal2.value = false : showModal1.value = false;
          uToastRef.value.show({
            type: "success",
            message: "状态设置成功"
          });
          getUserInfo();
        }).catch((err) => {
          if (status === 3) {
            showModal2.value = false;
          } else {
            showModal1.value = false;
            selectStatus.value === userInfo.value.dutyStatus;
          }
          uToastRef.value.show({
            type: "error",
            message: err
          });
        });
      }
    };
    const policeClock = () => {
      let qqmapsdk = new common_qqmapWxJssdk.QQMapWX({ key: config_app.qqmapKey });
      common_vendor.index.scanCode({
        onlyFromCamera: true,
        success: function(res) {
          if (res.result) {
            let communityId = res.result.substring(res.result.indexOf("=") + 1);
            api_community_index.communityById(Number(communityId)).then((res1) => {
              const { data: data2 } = res1;
              if (data2.id != userInfo.value.communityId) {
                uToastRef.value.show({
                  type: "error",
                  message: "请扫描您所属社区二维码！"
                });
              } else {
                if (data2.lat && data2.lnt && data2.clockInRange) {
                  common_vendor.index.getLocation({
                    type: "gcj02",
                    success: function(res2) {
                      const distance = calculateDistance(
                        res2.latitude,
                        res2.longitude,
                        data2.lat,
                        data2.lnt
                      );
                      if (Number(distance) <= Number((Number(data2.clockInRange) / 1e3).toFixed(5))) {
                        console.log("成功");
                        qqmapsdk.reverseGeocoder({
                          location: {
                            latitude: res2.latitude,
                            longitude: res2.longitude
                          },
                          success: function(result) {
                            api_police_clock.policeClockAdd({
                              policeUserId: userInfoStore.userInfo.userId,
                              clockAddress: result.result.address
                            }).then(async (res22) => {
                              if (res22.code === 200) {
                                common_vendor.index.navigateTo({
                                  url: "/subPages/police/clock/clockTip?status=1"
                                });
                              }
                            }).catch((err) => {
                              uToastRef.value.show({
                                type: "error",
                                message: err
                              });
                            });
                          },
                          fail: function(error) {
                            console.log(error);
                            uToastRef.value.show({
                              type: "error",
                              message: error.message
                            });
                          }
                        });
                      } else {
                        console.log("失败");
                        common_vendor.index.navigateTo({
                          url: "/subPages/police/clock/clockTip?status=0"
                        });
                      }
                    },
                    fail: function(_err) {
                      uToastRef.value.show({
                        type: "error",
                        message: "当前位置信息获取失败"
                      });
                    }
                  });
                } else {
                  uToastRef.value.show({
                    type: "error",
                    message: "请核实社区位置或打卡范围！"
                  });
                }
              }
            }).catch((err) => {
              uToastRef.value.show({
                type: "error",
                message: err
              });
            });
          } else {
            uToastRef.value.show({
              type: "error",
              message: "请扫码社区打卡二维码"
            });
          }
        }
      });
    };
    const calculateDistance = (lat1, lon1, lat2, lon2) => {
      let R = 6378.137;
      let radLat1 = lat1 * Math.PI / 180;
      let radLat2 = lat2 * Math.PI / 180;
      let a = radLat1 - radLat2;
      let b = lon1 * Math.PI / 180 - lon2 * Math.PI / 180;
      let s = 2 * Math.asin(
        Math.sqrt(
          Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
        )
      );
      s = s * R;
      s = Math.round(s * 1e4) / 1e4;
      return Number(s).toFixed(5);
    };
    const policeClockTipInfo = () => {
      api_police_index.getPoliceClockTip({ policeUserId: userInfoStore.userInfo.userId }).then((res) => {
        const { data: data2 } = res;
        if (data2.punchPrompt && data2.punchPrompt != 0) {
          uToastRef.value.show({
            type: "info",
            message: data2.punchPromptName
          });
        }
      }).catch((err) => {
        uToastRef.value.show({
          type: "error",
          message: err
        });
      });
    };
    const subscribeMsg = () => {
      common_vendor.index.requestSubscribeMessage({
        tmplIds: ["Jyz9Ae3NjjMDhTiNMcm3ygdZypx7ER-7At3JH4zryZo"],
        success(res) {
          if (res["Jyz9Ae3NjjMDhTiNMcm3ygdZypx7ER-7At3JH4zryZo"] === "accept") {
            console.log("用户同意订阅Jyz9Ae3NjjMDhTiNMcm3ygdZypx7ER");
            common_vendor.index.setStorageSync("isSubscribeMsg", true);
          } else {
            common_vendor.index.setStorageSync("isSubscribeMsg", false);
            uToastRef.value.show({
              type: "warning",
              message: "您拒绝了消息订阅，将无法接受消息通知！"
            });
          }
          policeClockTipInfo();
        },
        fail(err) {
          console.error("订阅消息失败", err);
        }
      });
    };
    const closeSubMsgPop = () => {
      showSubMsgPopup.value = false;
      common_vendor.index.setStorageSync("isSubscribeMsg", false);
      uToastRef.value.show({
        type: "warning",
        message: "您拒绝了消息订阅，将无法接受消息通知！"
      });
      policeClockTipInfo();
    };
    common_vendor.onLoad(() => {
      if (common_vendor.index.getStorageSync("isSubscribeMsg")) {
        showSubMsgPopup.value = false;
        policeClockTipInfo();
      } else {
        showSubMsgPopup.value = true;
      }
      getUserInfo();
    });
    common_vendor.onShow(() => {
      common_vendor.index.hideHomeButton();
      onNoReadStat();
    });
    common_vendor.onShareAppMessage((res) => {
      return {
        title: "码上朝阳",
        path: "/pages/crowd/home/index",
        imageUrl: "/static/logo.png",
        content: "我的码上朝阳小程序",
        mpId: "wxbaec7c3e7b23e9c4",
        type: 0
      };
    });
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.o(($event) => common_vendor.unref(utils_tools.onPageJump)("/subPages/police/mine/index")),
        b: common_vendor.p({
          userAvatar: common_vendor.unref(userInfoStore).userInfo.avatar,
          userName: common_vendor.unref(userInfoStore).userInfo.userName,
          showStatusTag: true,
          statusTagName: common_vendor.unref(userInfo).dutyStatus == 3 ? "不在岗" : "在岗"
        }),
        c: common_vendor.o(($event) => showStatusList.value = !common_vendor.unref(showStatusList)),
        d: common_vendor.unref(userInfo).dutyStatus != 3
      }, common_vendor.unref(userInfo).dutyStatus != 3 ? {
        e: common_vendor.o(($event) => {
          showModal1.value = true;
          showStatusList.value = false;
        }),
        f: common_vendor.o(($event) => {
          showModal2.value = true;
          showStatusList.value = false;
        })
      } : {}, {
        g: common_vendor.o(fileReport),
        h: common_vendor.n(common_vendor.unref(showStatusList) ? "status-list open w-_a_140rpx_a_ text-_a__a_0189FE_a_ text-_a_25rpx_a_ text-center absolute top-_a_50rpx_a_ right-0 z-10" : "status-list w-_a_140rpx_a_ text-_a__a_0189FE_a_ text-_a_25rpx_a_ text-center absolute top-_a_50rpx_a_ right-0 z-10"),
        i: common_vendor.f(common_vendor.unref(swipers), (item, index, i0) => {
          return {
            a: index,
            b: common_vendor.n(index === common_vendor.unref(current) && "indicator__dot--active")
          };
        }),
        j: common_vendor.o((e) => current.value = e.current),
        k: common_vendor.p({
          list: common_vendor.unref(swipers),
          circular: true,
          height: "360rpx",
          radius: "16rpx"
        }),
        l: common_vendor.o(onMenu),
        m: common_vendor.p({
          menuList: common_vendor.unref(menuList)
        }),
        n: common_vendor.o(($event) => common_vendor.unref(utils_tools.onPageJump)("/subPages/police/alarmRing/index")),
        o: common_vendor.n(common_vendor.unref(selectStatus) == 2 ? "active w-_a_194rpx_a_ h-_a_83rpx_a_ text-center leading-_a_83rpx_a_" : "unactive w-_a_194rpx_a_ h-_a_83rpx_a_ text-center leading-_a_83rpx_a_"),
        p: common_vendor.o(($event) => selectStatus.value = 2),
        q: common_vendor.n(common_vendor.unref(selectStatus) == 1 ? "active w-_a_194rpx_a_ h-_a_83rpx_a_ text-center leading-_a_83rpx_a_" : "unactive w-_a_194rpx_a_ h-_a_83rpx_a_ text-center leading-_a_83rpx_a_"),
        r: common_vendor.o(($event) => selectStatus.value = 1),
        s: common_vendor.o(($event) => editStatus(common_vendor.unref(selectStatus))),
        t: common_vendor.o(($event) => showModal1.value = false),
        v: common_vendor.o(($event) => showModal1.value = false),
        w: common_vendor.p({
          show: common_vendor.unref(showModal1),
          title: "在岗状态设置",
          closeOnClickOverlay: true,
          showCancelButton: true,
          confirmColor: "#ffffff",
          cancelColor: "#000000"
        }),
        x: common_vendor.o(($event) => editStatus(3)),
        y: common_vendor.o(($event) => showModal2.value = false),
        z: common_vendor.o(($event) => showModal2.value = false),
        A: common_vendor.p({
          show: common_vendor.unref(showModal2),
          title: "提示",
          content: "确认下班后状态将变为【不在岗】？",
          contentTextAlign: "center",
          closeOnClickOverlay: true,
          showCancelButton: true,
          confirmColor: "#ffffff",
          cancelColor: "#000000"
        }),
        B: common_vendor.o(($event) => closeSubMsgPop()),
        C: common_vendor.o(($event) => {
          showSubMsgPopup.value = false;
          subscribeMsg();
        }),
        D: common_vendor.p({
          show: common_vendor.unref(showSubMsgPopup),
          title: "消息订阅",
          content: "是否允许订阅办事咨询和工作任务的微信服务消息推送功能？",
          showCancelButton: true
        }),
        E: common_vendor.sr(uToastRef, "0c81b12d-6", {
          "k": "uToastRef"
        })
      });
    };
  }
});
_sfc_defineComponent.__runtimeHooks = 2;
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_defineComponent, [["__scopeId", "data-v-0c81b12d"]]);
wx.createPage(MiniProgramPage);
